package cn.tedu.submarine;
import javax.swing.ImageIcon;
import java.util.Random;
import java.awt.Graphics;
//object(类),sea(海洋)
public abstract class SeaObject {
    public static  final int LIVE =0;//活着
    public static  final int DEAD =1;//死了
    protected int state = LIVE;//当前窗台(默认活着的)

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    //给鱼雷,水雷,侦查潜艇提供的构造方法,
    public SeaObject(int width,int height){
        this.width =width;
        this.height=height;
        x = -width;//侦查潜艇的宽
        Random rd = new Random();
        y = rd.nextInt(World.HEIGHT-height-150+1)+150;
        speed = rd.nextInt(3)+1;
    }
    //给战舰,深水炸弹,鱼雷提供的构造方法
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width=width;
        this.height = height;
        this.x=x;
        this.y=y;
        this.speed=speed;
    }


    //移动方法
    public abstract void move();

    //获取对象的图片,,返回值:一张图片
    public  abstract  ImageIcon getImage();

    //判断对象是否活着
    public boolean isLive(){
        return state==LIVE;//若当前状态为LIVE,则返回true表示活着,否则返回false死了
    }
    //判断对象是否死了
    public boolean isDead(){
        return state==DEAD;//若当前状态为DEAD,则返回true表示死了,否则返回false活着
    }
    //画图
    public void paintImage(Graphics g){
        if (this.isLive()){//若活着
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    /**检测潜艇越界*/
    public boolean isOutdfBounds(){
        return this.x>=World.WIDTH;//潜艇的x>=窗口的宽,既为越界
    }

    /**检测碰撞方法*/
    public boolean isHit(SeaObject other){
        //this:指潜艇,other:指炸弹
        int x1 = this.x-other.x;//潜艇的x+潜艇的宽
        int x2 = this.x+other.x;//潜艇的x+潜艇的宽
        int y1 = this.y-other.y;//潜艇的y-潜艇高
        int y2 = this.y+other.y;//潜艇的y+潜艇高
        int x = other.x;//炸弹的x
        int y = other.y;//炸弹的y
        return  x>=x1 && x<=x2
                &&
                y>=y1 &&y<=y2;//x在x1和x2之间,y在y1和y2之间,即为碰撞
    }

    /**海洋对象去死*/
    public void goDead(){
        state = DEAD;//将对象状态设置为DEAD死了
    }
}
